<template>
	<view class="reset-password">
		<view class="login-form">
			<uni-forms label-position="top" ref="form" :modelValue="formData" :rules="rules">
				<uni-forms-item label="旧密码" name="oldPassword">
					<uni-easyinput type="password"  class="login-box-input" placeholder="旧密码" v-model.trim="formData.oldPassword"/>
				</uni-forms-item>
				<uni-forms-item label="新密码" name="newPassword">
					<uni-easyinput type="password" class="login-box-input" placeholder="新密码" v-model.trim="formData.newPassword"/>
				</uni-forms-item>
				<uni-forms-item label="确认密码" name="confirmNewPassword">
					<uni-easyinput type="password" class="login-box-input" placeholder="确认密码" v-model.trim="formData.confirmNewPassword"/>
				</uni-forms-item>
			</uni-forms>
			<button @click="submit">保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				//版本号
				version: this.$constant.version,
				//表单数据
				formData:{
					oldPassword: "", //旧密码
					newPassword:"", //新密码
					confirmNewPassword: "" //确认密码
				},
				//校验规则
				rules: {
					oldPassword: {
						rules: [{
							required: true,
							errorMessage: "请填写旧密码！"
						}]
					},newPassword: {
						rules: [{
							required: true,
							errorMessage: "请填写新密码！"
						}]
					},confirmNewPassword: {
						rules: [{
							required: true,
							errorMessage: "请填写确认密码！"
						}]
					}
					
				}
			}
		},
		onLoad(){
			//获取token
			let token = uni.getStorageSync("token");
			console.log(!token);
			if( !token){
				// uni.showToast({
				// 	title: "请先登录！！"
				// })
				uni.redirectTo ({
					url: "/pages/login/login"
				});
			}
		},
		methods: {
			//触发提交表单
			submit(){
				// console.log(1)
				this.$refs.form.validate().then(async res => {
					if( this.formData.oldPassword == ""){
						uni.showToast({
						  title: "请填写旧密码！",
						  icon: "none",
						  duration: 2000,
						});
						return;
					}
					if( this.formData.newPassword == ""){
						uni.showToast({
						  title: "请填写新密码！",
						  icon: "none",
						  duration: 2000,
						});
						return;
					}
					if( this.formData.confirmNewPassword == ""){
						uni.showToast({
						  title: "请填写确认密码！",
						  icon: "none",
						  duration: 2000,
						});
						return;
					}
					if( this.formData.confirmNewPassword != this.formData.newPassword){
						console.log(1)
						uni.showToast({
						  title: "确认密码与新密码不一致！",
						  icon: "none",
						  duration: 2000,
						});
						return;
					}
					
					//进行修改操作
					const resp = await this.$myRequest({
						url: "/api/student/user/updatePassword",
						method: "PUT",
						data: this.formData
					});
					if(resp.data.code == 200){
						uni.showToast({
							title: resp.data.message
						})
						uni.removeStorageSync("token");
						uni.redirectTo({
							url: "/pages/login/login"
						})
					}
					uni.showToast({
						title: resp.data.message
					});
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})
				
			}
		}
	}
</script>

<style lang="scss">
page{
	background-color: #fff;
}
.reset-password{
	display: flex;
	justify-content: center;
	flex-direction: column;
	align-items: center;
	width:100%;
	box-sizing: border-box;
	.login-form{
		width: 92%;
		width:94%;
		margin-top: 50rpx;
		.login-box-input{
			background-color:#F8F8F8 ;
		}
		button{
			margin-top: 80rpx;
			background: #66BB6A;
		}
	}
}
</style>
